package com.ninetaildemonfox.zdl.socialproject.mvp.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.hyphenate.easeui.utils.Preferences;
import com.ninetaildemonfox.zdl.ninetaildemonfox.base.BaseFragment;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.SpApplication;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.CityListSelectActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.SearchActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.NewsViewPagerFragmentAdapter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.fragment.DHomeFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import static android.app.Activity.RESULT_OK;

/**
 * @author NineTailDemonFox
 * @date 2019/7/15 14:44
 * 功能描述： 约会大厅
 * 联系方式：1037438704@qq.com
 */
public class DatingHallFragment extends BaseFragment {

    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.article_viewpager)
    ViewPager articleViewpager;
    @BindView(R.id.text_city)
    TextView textCity;
    @BindView(R.id.text_search)
    TextView textSearch;
    public static String city;
    private List<BaseFragment> mFragmentList;

    @BindView(R.id.image_switch_sex)
    ImageView imageSwitchSex;
    private int count = 1;
    private String sex;

    private NewsViewPagerFragmentAdapter adapter;
    private String[] TITLE = new String[]{"附近", "新来", "认证"};
    private String[] TITLE2 = new String[]{"附近", "会员"};
    DHomeFragment dHomeFragment1, dHomeFragment2, dHomeFragment3;
    private String jingdu, weidu;

    @Override
    public void onInitView(Bundle savedInstanceState) {
        dHomeFragment1 = DHomeFragment.newInstance();
        dHomeFragment2 = DHomeFragment.newInstance();
        dHomeFragment3 = DHomeFragment.newInstance();
        jingdu = SpApplication.LONGITUDE;
        weidu = SpApplication.LATITUDE;

        initView();
        setAdapter();
    }

    private void setAdapter() {
        sex = Preferences.getInstance().getString(me, "user", "sex");
        if (sex.equals("1")) {
            imageSwitchSex.setImageResource(R.mipmap.ic_switch_boy);
            adapter.addTitle(TITLE);
            dHomeFragment1.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 1);
            dHomeFragment2.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 2);
            dHomeFragment3.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 3);
            mFragmentList.add(dHomeFragment1);
            mFragmentList.add(dHomeFragment2);
            mFragmentList.add(dHomeFragment3);
        } else {
            imageSwitchSex.setImageResource(R.mipmap.ic_switch_sex);
            textSearch.setText("搜索男士昵称");
            adapter.addTitle(TITLE2);
            dHomeFragment1.getCity(jingdu, weidu, textCity.getText().toString().trim(), "1", 1);
            dHomeFragment2.getCity(jingdu, weidu, textCity.getText().toString().trim(), "1", 2);
            mFragmentList.add(dHomeFragment1);
            mFragmentList.add(dHomeFragment2);
        }
        //实例化适配器
        //设置适配器
        articleViewpager.setAdapter(adapter);
        articleViewpager.setOffscreenPageLimit(mFragmentList.size());
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setupWithViewPager(articleViewpager);
    }

    private void initView() {
        mFragmentList = new ArrayList<>();
        adapter = new NewsViewPagerFragmentAdapter(getChildFragmentManager(), mFragmentList);
    }


    @OnClick({R.id.tabLayout, R.id.article_viewpager, R.id.text_city, R.id.text_search, R.id.image_switch_sex})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tabLayout:

                break;
            case R.id.article_viewpager:
                Toast.makeText(mContext, "点击了", Toast.LENGTH_SHORT).show();
                break;
            case R.id.text_city:
                startActivityForResult(new Intent(getActivity(), CityListSelectActivity.class), 1);
                break;
            case R.id.text_search:
                SearchActivity.start(getContext());
                break;
            case R.id.image_switch_sex:
                if (sex.equals("1")) {
                    imageSwitchSex.setImageResource(R.mipmap.ic_switch_sex);
                    sex = "2";
                    adapter = new NewsViewPagerFragmentAdapter(getChildFragmentManager(), mFragmentList);
                    mFragmentList.clear();
                    adapter.addTitle(TITLE2);
                    dHomeFragment1.getCity(jingdu, weidu, textCity.getText().toString().trim(), "1", 1);
                    dHomeFragment2.getCity(jingdu, weidu, textCity.getText().toString().trim(), "1", 2);
                    mFragmentList.add(dHomeFragment1);
                    mFragmentList.add(dHomeFragment2);
                    //实例化适配器
                    //设置适配器
                    articleViewpager.setAdapter(adapter);
                    articleViewpager.setOffscreenPageLimit(mFragmentList.size());
                    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
                    tabLayout.setTabMode(TabLayout.MODE_FIXED);
                    tabLayout.setupWithViewPager(articleViewpager);
                } else {
                    imageSwitchSex.setImageResource(R.mipmap.ic_switch_boy);
                    sex = "1";
                    mFragmentList.clear();
                    adapter = new NewsViewPagerFragmentAdapter(getChildFragmentManager(), mFragmentList);
                    adapter.addTitle(TITLE);
                    dHomeFragment1.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 1);
                    dHomeFragment2.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 2);
                    dHomeFragment3.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 3);
                    mFragmentList.add(dHomeFragment1);
                    mFragmentList.add(dHomeFragment2);
                    mFragmentList.add(dHomeFragment3);
                    articleViewpager.setAdapter(adapter);
                    articleViewpager.setOffscreenPageLimit(mFragmentList.size());
                    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
                    tabLayout.setTabMode(TabLayout.MODE_FIXED);
                    tabLayout.setupWithViewPager(articleViewpager);
                }
                break;
            default:
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_dating_hall;
    }

    public static DatingHallFragment newInstance() {
        return new DatingHallFragment();
    }

    //结果处理函数，当从secondActivity中返回时调用此函数
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String city = bundle.getString("city");
            if (city.equals("附近")) {
                jingdu = SpApplication.LONGITUDE;
                weidu = SpApplication.LATITUDE;
            } else if (city.equals("天津市")) {
                jingdu = SpApplication.LONGITUDE;
                weidu = SpApplication.LATITUDE;
            } else {
//                jingdu = bundle.getString("getLng");
//                weidu = bundle.getString("getLat");
                jingdu = SpApplication.LONGITUDE;
                weidu = SpApplication.LATITUDE;
            }
            textCity.setText(city);
            if (sex.equals("1")) {
                dHomeFragment1.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 1, "1");
                dHomeFragment2.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 2, "1");
                dHomeFragment3.getCity(jingdu, weidu, textCity.getText().toString().trim(), "2", 3, "1");
            } else {
                dHomeFragment1.getCity(jingdu, weidu, textCity.getText().toString().trim(), "1", 1, "1");
                dHomeFragment2.getCity(jingdu, weidu, textCity.getText().toString().trim(), "1", 2, "1");
            }
        }
    }
}